package com.macro.mall.product.util;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.druid.support.json.JSONUtils;
import org.dozer.Mapper;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class DozerUtils {

    /**
     * 封装dozer处理集合的方法：List<S> --> List<T>
     */
    public static <T, S> List<T> mapList(final Mapper mapper, List<S> sourceList, Class<T> targetObjectClass) {
        if (CollectionUtils.isEmpty(sourceList))
            return new ArrayList<>();

        List<T> targetList = new ArrayList<T>();
        for (S s : sourceList) {
            targetList.add(mapper.map(s, targetObjectClass));
        }
        return targetList;
    }

    public static<S, B, T> T map(final Mapper mapper, S source, Class<B> boClass, Class<T> targetObjectClass) {
        B bo = mapper.map(source, boClass);
        return mapper.map(bo, targetObjectClass);
    }
    public static<S, B, T> T map(final Mapper mapper, S source,  Class<T> targetObjectClass) {
        return mapper.map(source, targetObjectClass);
    }

//    public static void main(String[] args) {
//        JSONObject obj = new JSONObject();
//        obj.put("tempType","11");
//        JSONArray array = new JSONArray();
//        JSONObject obj1 = new JSONObject();
//        obj1.put("time","09:00-11:30");
//        array.add(obj1);
//        obj1 = new JSONObject();
//        obj1.put("time","13:00-17:30");
//        array.add(obj1);
//        obj.put("timeList",array);
//        obj.put("tempName","库存测试模板");
//
//        String objStr = JSONUtils.toJSONString(obj);
//        System.out.println("******************="+objStr);
//    }
}
